﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Common;

namespace MxWeiXinPF.Web.admin.order
{
    public partial class order_edit : Web.UI.ManagePage
    {
        private int id = 0;
        protected Model.orders model = new Model.orders();

        protected bool permit = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                this.id = MXRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back", "Error");
                    return;
                }
                if (!new BLL.orders().Exists(this.id))
                {
                    JscriptMsg("记录不存在或已被删除！", "back", "Error");
                    return;
                }
                if (!Page.IsPostBack)
                {
                    ChkAdminLevel("order_list", MXEnums.ActionEnum.View.ToString()); //检查权限
                    permit = ChkAdminLevelEx("order_list", MXEnums.ActionEnum.Edit.ToString());
                    permit = btnConfirm.Visible;
                    permit = btnConfirm.Visible;
                    permit = btnConfirm.Visible;
                    permit = btnConfirm.Visible;
                    permit = btnConfirm.Visible;
                    permit = btnConfirm.Visible;
                    ShowInfo(this.id);

                }
            }
            catch (Exception ex)
            {
                
            }
  
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            MxWeiXinPF.Model.wx_userweixin weiXinCode = base.GetWeiXinCode();
            this.model = new MxWeiXinPF.BLL.orders().GetModel(_id);
            this.rptList.DataSource = this.model.order_goods;
            this.rptList.DataBind();
            if (this.model.user_id > 0)
            {
                MxWeiXinPF.Model.users model = new MxWeiXinPF.BLL.users().GetModel(this.model.user_id);
                if (model != null)
                {
                    MxWeiXinPF.Model.user_groups _groups = new MxWeiXinPF.BLL.user_groups().GetModel(model.group_id);
                    if (_groups != null)
                    {
                        this.dlUserInfo.Visible = true;
                        this.lbUserName.Text = model.user_name;
                        this.lbUserGroup.Text = _groups.title;
                        this.lbUserDiscount.Text = _groups.discount.ToString() + " %";
                        this.lbUserAmount.Text = model.amount.ToString();
                        this.lbUserPoint.Text = model.point.ToString();
                    }
                }
            }
            switch (this.model.status)
            {
                case 1:
                    if (this.model.payment_status <= 0)
                    {
                        this.btnConfirm.Visible = this.btnCancel.Visible = this.btnEditAcceptInfo.Visible = true;
                        break;
                    }
                    this.btnPayment.Visible = this.btnCancel.Visible = this.btnEditAcceptInfo.Visible = true;
                    break;

                case 2:
                    if (this.model.express_status != 1)
                    {
                        if (this.model.express_status == 2)
                        {
                            this.btnComplete.Visible = this.btnCancel.Visible = true;
                        }
                    }
                    else
                    {
                        this.btnExpress.Visible = this.btnCancel.Visible = this.btnEditAcceptInfo.Visible = true;
                    }
                    this.btnEditRemark.Visible = true;
                    goto Label_02C2;

                case 3:
                    this.btnInvalid.Visible = this.btnEditRemark.Visible = true;
                    goto Label_02C2;

                default:
                    goto Label_02C2;
            }
            this.btnEditRemark.Visible = this.btnEditRealAmount.Visible = this.btnEditExpressFee.Visible = this.btnEditPaymentFee.Visible = true;
        Label_02C2:
            if ((this.model.express_status == 2) && (this.model.express_no.Trim().Length > 0))
            {
                MxWeiXinPF.Model.express express = new MxWeiXinPF.BLL.express().GetModel(this.model.express_id);
                MxWeiXinPF.Model.orderconfig orderconfig = new MxWeiXinPF.BLL.orderconfig().loadConfig();
                if (((express != null) && (express.express_code.Trim().Length > 0)) && (orderconfig.kuaidiapi != ""))
                {
                    string input = Utils.HttpGet(string.Concat(new object[] { orderconfig.kuaidiapi, "?id=", orderconfig.kuaidikey, "&com=", express.express_code, "&nu=", this.model.express_no, "&show=", orderconfig.kuaidishow, "&muti=", orderconfig.kuaidimuti, "&order=", orderconfig.kuaidiorder }));
                    if (input != null)
                    {
                        this.litExpressDetail.Text = Utils.ToHtml(input);
                    }
                }
            }
        }
        #endregion

        protected string GetCouponInfo(int orderid)
        {
            string info = "未使用优惠券";
            BLL.wx_shop_coupon_data bcoupondata = new BLL.wx_shop_coupon_data();

            List<Model.wx_shop_coupon_data> datas = bcoupondata.GetModelList(string.Format(" ordersn={0} ",model.id));
            if (datas!= null && datas.Count > 0)
            {
                Model.wx_shop_coupon_data mc = datas[0];
                Model.wx_shop_coupon mcoupon = new BLL.wx_shop_coupon().GetModel(mc.couponid);
                if (mcoupon != null)
                {
                    info = string.Format("优惠券编号:{0},面值:{1},描述:{2}",mcoupon.id,mcoupon.money,mcoupon.description);
                }
            }
            return info;
        }

    }
}